package com.lc.ck.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.ck.common.QueryPageParam;
import com.lc.ck.common.Result;
import com.lc.ck.entity.Goods;
import com.lc.ck.entity.Record;
import com.lc.ck.entity.User;
import com.lc.ck.service.GoodsService;
import com.lc.ck.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/listPlist")//返回数据封装
    public Result listPlist(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String goodstype = (String)param.get("goodstype");
        String storage = (String)param.get("storage");
        String roleId = (String)param.get("roleId");
        String userId = (String)param.get("userid");


        Page<Record> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        QueryWrapper<Record> queryWrapper = new QueryWrapper();//子类不可用lambdaQueryWrapper
        queryWrapper.apply(" a.goods=b.id and b.storage=c.id and b.goodstype=d.id ");//占位符（${ew.customSqlSegment}）会自动增加一个where 两个where会报错
        if( "2".equals(roleId)){
            queryWrapper.apply("a.userid=" + userId);
        }
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){//非空判断和非 null 判断
//            lambdaQueryWrapper.like(Record::getName,name);
            queryWrapper.like("b.name",name);
        }
        if(StringUtils.isNotBlank(goodstype) && !"null".equals(goodstype)){//非空判断和非 null 判断
//            lambdaQueryWrapper.eq(Record::getStorage,storage);
            queryWrapper.like("d.id",goodstype);

        }
        if(StringUtils.isNotBlank(storage) && !"null".equals(storage)){//非空判断和非 null 判断
//            lambdaQueryWrapper.eq(Record::getGoodstype,goodstype);
            queryWrapper.like("c.id",storage);

        }


        IPage result = recordService.pageCC(page,queryWrapper);

        return Result.suc(result.getRecords(),result.getTotal());//成功时将数据和总数返回
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Record  record){
       Goods goods =  goodsService.getById(record.getGoods());
       int n = record.getCount();
       if("2".equals(record.getAction())){  //出库
           n = -n;
           record.setCount(n);
       }
       int num = goods.getCount()+n;
       goods.setCount(num);
        goodsService.updateById(goods);//record入库操作增加的数量，将查出goods表的数量再加上入库增加的数量

        return recordService.save(record)?Result.suc():Result.fail();
    }
}
